package com.lc.controller;

import com.lc.mapper.OrderMapper;
import com.lc.pojo.Order;
import com.lc.pojo.Shop;
import com.lc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/list")
    public String orderList(Model model){
        // 查询舰船列表并返回前台
        List<Order> orders = orderMapper.queryOrderList();
        model.addAttribute("orders", orders);
        return "mange/order";
    }
    @RequestMapping("/toEdit/{order_number}")
    public String toAdd(@PathVariable String order_number, Model model) throws Exception {

        Order order = orderMapper.queryOrderByNum(order_number);
        model.addAttribute("order", order);
        return "mange/editOrder";

    }

    @RequestMapping("/toEditState/{order_number}")
    public String toEditState(@PathVariable String order_number, Model model) throws Exception {
        Order order = orderMapper.queryOrderByNum(order_number);
        model.addAttribute("order", order);
        return "mange/editOrderState";
    }

    @RequestMapping("/toAdd")
    public String toAdd() throws Exception {
        return "mange/addOrder";

    }
    @RequestMapping("/add")
    public String addOrder(HttpServletRequest request,Order order) throws Exception {
        Order orderNew = new Order();
        BeanUtils.copyProperties(order,orderNew);
        Boolean succ = orderService.addOrder(orderNew);
        if (succ) {
            String successMsg = URLEncoder.encode("增加成功", "UTF-8");
            return "redirect:/order/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("增加失败", "UTF-8");
            return "redirect:/order/list?successMsg=" + errorMsg;
        }
    }
    @RequestMapping("/update")
    public String updateOrder(HttpServletRequest request, Order order) throws Exception {
        Order orderNew = new Order();
        BeanUtils.copyProperties(order,orderNew);
        Boolean succ = orderService.updateOrder(orderNew);
        if (succ) {
            String successMsg = URLEncoder.encode("修改成功", "UTF-8");
            return "redirect:/order/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("修改失败", "UTF-8");
            return "redirect:/order/list?successMsg=" + errorMsg;
        }
    }
    @RequestMapping("/delete/{order_number}")
    public String deleteOrder(@PathVariable String order_number) throws Exception {
        int succ = orderMapper.deleteOrderByNum(order_number);
        if (succ==1) {
            String successMsg = URLEncoder.encode("删除成功", "UTF-8");
            return "redirect:/order/list?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("删除失败", "UTF-8");
            return "redirect:/order/list?successMsg=" + errorMsg;
        }
    }
}
